<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    id: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "100%"
    },
    chartData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      chart: null,
      index: 5,
      currentData: [],
      interval: null
    };
  },
  mounted() {
    // 初始化图表
    this.chart = this.createChart();
    // 动态数据 +1
    this.interval = setInterval(() => {
      this.index += 1;
    }, 7000);
  },
  watch: {
    chartData: {
      handler(newVal) {
        this.$nextTick(() => {
          this.index = 5;
          this.currentData = newVal.slice(0, 5);
          this.renderChart(this.currentData);
        });
      },
      immediate: true,
      deep: true
    },
    index(newVal) {
      this.currentData.shift();
      const currentData = [...this.currentData];
      const chartDataLen = this.chartData.length;
      const newItem = this.chartData[newVal % chartDataLen];
      this.currentData = [...currentData, newItem];
      const { xAxisData, seriesData } = this.generateChartData(
        this.currentData
      );
      this.chart.setOption({
        xAxis: {
          data: xAxisData
        },
        series: [
          {
            data: seriesData
          }
        ]
      });
    }
  },
  methods: {
    createChart() {
      return echarts.init(document.getElementById(this.id));
    },
    renderChart(data) {
      const chartData = this.generateChartData(data);
      // 根据数据渲染echart图表
      const option = this._createChartOption(chartData);
      if (this.chart) {
        this.chart.setOption(option);
      }
    },
    generateChartData(data) {
      const xAxisData = [];
      const seriesData = [];
      data.forEach(el => {
        xAxisData.push(el.name);
        seriesData.push(el.count);
      });
      return { xAxisData, seriesData };
    },
    _createChartOption({ xAxisData, seriesData }) {
      seriesData = seriesData;
      const colors = [
        "#3286FB",
        "#77DE73",
        "#97AED8",
        "#F8C748",
        "#E8684A",
        "#5ECEFA",
        "#A479EB",
        "#FF9D4D",
        "#38C0BF",
        "#FF99A4"
      ];
      const option = {
        color: colors,
        tooltip: {
          trigger: "axis",
          axisPointer: {
            animation: false
          },
          axisPointer: {
            lineStyle: {
              color: "rgba(135,188,255, 0.3)"
            }
          }
        },
        xAxis: {
          type: "category",
          data: xAxisData,
          axisLine: {
            show: false
          },
          axisLabel: {
            color: "#87BCFF"
          }
        },
        yAxis: {
          type: "value",
          splitNumber: 4,
          axisLine: {
            show: false
          },
          axisLabel: {
            color: "#87BCFF"
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "rgba(135,188,255, 0.3)"
            }
          }
        },
        grid: {
          top: 20,
          bottom: 25,
          left: 50,
          right: 5
        },
        series: [
          {
            data: seriesData,
            type: "line",
            label: {
              show: true,
              position: "top"
            },
            smooth: true
          }
        ]
      };
      return option;
    },
    beforeDestroy() {
      this.chart.dispose();
      this.chart = null;
    }
  },
  beforeDestroy() {
    clearInterval(this.interval);
  }
};
</script>

<style></style>
